mod common;
use crate::common::*;
use crate::rule::{ck_rule, rule_data};
use base::base::VMap;
use serde_json::json;

#[tokio::test]
async fn test_rule_1() {
    let val = rule_data();

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "",
                "name": "name_code1",
                "v": [
                {"type": "fix", "name": "fix", "keyv": "AAA", "num": 3},
                ]
            }
        }),
        "code",
        "add",
        e_miss("sn"),
    )
    .await;

    do_ret(
        pos!(),
        json!({
            "line": "1",
        }),
        "code",
        "list",
        e_node(),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("code1").unwrap()
        }),
        "code",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("code2").unwrap()
        }),
        "code",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "te1",
                "name": "name_te1",
                "code": val.ck_val("tcode").unwrap().ck_val("code").unwrap(),
            }
        }),
        "tech",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("task").unwrap()
        }),
        "task",
        "add",
    )
    .await;

    ok_ret(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
        }),
        "task",
        "get",
        val.ck_val("task").unwrap().clone(),
    )
    .await;

    ck_rule(
        pos!(),
        json!({
            "all": ["code1", "code2"],
            "run": [val.ck_val("run").unwrap()],
            "code1": val.ck_val("code1").unwrap(),
            "code2": val.ck_val("code2").unwrap(),
            "tcode": val.ck_val("tcode").unwrap(),
            "task": val.ck_val("task").unwrap(),
        }),
    )
    .await;

    let ret = dourl(
        json!({
            "line": "1",
            "task": "task1",
            "wsn": "M010",
            "pure": true,
        }),
        "code",
        "getpsn",
    )
    .await
    .expect("getpsn");

    let ret = ret.k_obj("data").unwrap();

    let tech = ret.get("tech").unwrap();

    assert_eq!(json!(tech), json!("te1"));

    let c1 = ret.get("c1").unwrap().as_str().unwrap().to_string();
    let c2 = ret.get("c1").unwrap().as_str().unwrap().to_string();
    let psn = ret.get("psn").unwrap().as_str().unwrap().to_string();

    assert_eq!(c1.len(), 18);
    assert_eq!(c2.len(), 18);
    assert_eq!(psn.len(), 18);

    let ret = dourl(
        json!({
            "line": "1",
            "task": "task1",
            "wsn": "M010",
            "pure": true,
        }),
        "code",
        "getpsn",
    )
    .await
    .expect("getpsn");

    let ret = ret.k_obj("data").unwrap();

    let c11 = ret.get("c1").unwrap().as_str().unwrap().to_string();
    let c22 = ret.get("c1").unwrap().as_str().unwrap().to_string();
    let psn1 = ret.get("psn").unwrap().as_str().unwrap().to_string();

    assert_eq!(psn1.len(), 18);
    assert_eq!(c11.len(), 18);
    assert_eq!(c22.len(), 18);

    let ret = dourl(
        json!({
            "line": "1",
            "task": "task1",
            "wsn": "M020",
            "pure": true,
        }),
        "code",
        "getpsn",
    )
    .await
    .expect("getpsn");

    let ret = ret.k_obj("data").unwrap();

    let tech = ret.get("tech").unwrap();

    assert_eq!(json!(tech), json!("te1"));

    let ac1 = ret.get("c1").unwrap().as_str().unwrap().to_string();
    let ac2 = ret.get("c1").unwrap().as_str().unwrap().to_string();
    let apsn = ret.get("psn").unwrap().as_str().unwrap().to_string();

    assert_eq!(apsn.len(), 18);
    assert_eq!(ac1.len(), 18);
    assert_eq!(ac2.len(), 18);

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "task": "task1",
            "wsn": "M020",
            "pure": true,
        }),
        "code",
        "getpsn",
        e_code("no psn"),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("task2").unwrap()
        }),
        "task",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("task3").unwrap()
        }),
        "task",
        "add",
    )
    .await;

    let ret = dourl(
        json!({
            "line": "1",
            "wsn": "M020",
            "pure": true,
        }),
        "code",
        "getpsn",
    )
    .await
    .expect("getpsn");

    let ret = ret.k_obj("data").unwrap();

    let tech = ret.get("tech").unwrap();

    assert_eq!(json!(tech), json!("te1"));

    let ac1 = ret.get("c1").unwrap().as_str().unwrap().to_string();
    let ac2 = ret.get("c1").unwrap().as_str().unwrap().to_string();
    let apsn = ret.get("psn").unwrap().as_str().unwrap().to_string();

    assert_eq!(apsn.len(), 18);
    assert_eq!(ac1.len(), 18);
    assert_eq!(ac2.len(), 18);

    let ret = dourl(
        json!({
            "line": "1",
            "wsn": "M020",
            "pure": true,
        }),
        "code",
        "getpsn",
    )
    .await
    .expect("getpsn");

    let ret = ret.k_obj("data").unwrap();

    let tech = ret.get("tech").unwrap();

    assert_eq!(json!(tech), json!("te1"));

    let ac1 = ret.get("c1").unwrap().as_str().unwrap().to_string();
    let ac2 = ret.get("c1").unwrap().as_str().unwrap().to_string();
    let apsn = ret.get("psn").unwrap().as_str().unwrap().to_string();

    assert_eq!(apsn.len(), 18);
    assert_eq!(ac1.len(), 18);
    assert_eq!(ac2.len(), 18);

    do_ret(
        pos!(),
        json!({
            "line": "1",
            "task": "task1",
            "wsn": "M020",
            "pure": true,
        }),
        "code",
        "getpsn",
        e_code("no psn"),
    )
    .await;
}
